import os

import yaml
from flask import Config

from common.server.app_instance import app

# 读取配置文件加载到app.config中
config_path = os.path.join(os.path.dirname(__file__), '../../app.yaml')
with open(config_path, 'r') as f:
    config = yaml.safe_load(f)
    app.config.update(config)


def get_config(conf_path: str, default=None):
    """
    从配置文件中读取配置信息。
    """
    config = app.config
    path_arr = conf_path.split('.')
    for path in path_arr:
        if config is None:
            return default
        if not isinstance(config, Config) and not isinstance(config, dict):
            return default
        config = config.get(path)
    return config
